package com.woniuxy.user.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.core.annotation.Permission;
import com.woniuxy.core.exception.ParamException;
import com.woniuxy.core.util.Result;
import com.woniuxy.oss.util.FileUtil;
import com.woniuxy.oss.util.OSSUtil;
import com.woniuxy.user.service.exception.FileException;
import com.woniuxy.user.ossform.UpdateUserHeaderForm;
import com.woniuxy.user.service.UserInfoService;
import com.woniuxy.user.dto.UserInfoDto;
import com.woniuxy.user.form.RealNameForm;
import com.woniuxy.user.form.UpdateUserInfoForm;
import com.woniuxy.user.util.TokenUtil;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ...
 * @since 2024年05月22日
 */
@RestController
@RequestMapping("/userInfo")
@Api(tags="用户信息接口")
public class UserInfoController {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private OSSUtil ossUtil;

    @Resource
    private TokenUtil tokenUtil;

    /**
     * 完善个人信息
     * @param token
     * @param form 完善用户信息参数
     * @return
     */
    @PostMapping("/updateUserInfo.user")
    @ApiOperation("完善个人信息")
    public Result updateUserInfo(@RequestHeader String token, @RequestBody UpdateUserInfoForm form){
        if(ObjectUtil.isEmpty(form.getNickName())) throw new ParamException(10001,"参数有误");
        Integer userId = tokenUtil.getUserId(token);
        userInfoService.updateUserInfo(userId,form);
        return Result.success("完善个人信息成功",null);
    }

    /**
     * 实名认证
     * @param token
     * @param form 完善用户信息参数
     * @return
     */
    @PostMapping("/realName.user")
    @ApiOperation("实名认证")
    public Result realName(@RequestHeader String token, @RequestBody  RealNameForm form) throws IOException {
        if(ObjectUtil.isEmpty(form.getRealName())||ObjectUtil.isEmpty(form.getIdCard())) throw new ParamException(10001,"参数有误");
        Integer userId = tokenUtil.getUserId(token);
        userInfoService.realName(userId,form);
        return Result.success("实名认证成功",null);
    }

    /**
     * 上传头像
     * @param form 用户上传头像图片
     * @param token
     * @return
     * @throws IOException
     */
    @PostMapping("/updateUserHeader.user")
    @ApiOperation("上传头像")
    public Result updateUserHeader(UpdateUserHeaderForm form ,@RequestHeader String token) throws IOException {
        if(!FileUtil.isImageFile(form.getFile().getInputStream())){
            throw  new FileException(3001,"文件类型错误");
        }
        String oldName = form.getFile().getOriginalFilename();
        String suffix = oldName.substring(oldName.lastIndexOf("."));
        String name= UUID.randomUUID().toString();
        String filePath=ossUtil.uploadFile(form.getFile().getInputStream(),name+suffix,"woniuxy108cr");
        if(filePath==null) throw new FileException(3002,"上传头像失败");
        Integer userId = tokenUtil.getUserId(token);
        userInfoService.updateUserHeader(userId,filePath);
        return Result.success("更新头像成功",filePath);
    }

    /**
     * 分页查询所有用户
     * @param pageNum 当前页
     * @param pageSize 每页几条
     * @return
     */
    @GetMapping("/loadAllUserInfo")
    @ApiOperation("分页查询所有用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页"),
            @ApiImplicitParam(name = "pagesize",value = "每页几条")
    })
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = UserInfoDto.class)
    })
    @Permission(roles = {"超级管理员","管理员"})
    public Result loadAllUserInfo(Integer pageNum, Integer pageSize,String condition){
    Page<UserInfoDto> page=userInfoService.loadAllUserInfo(pageNum,pageSize,condition);
    return  Result.success("分页查询所有用户成功",page);
    }

    /**
     * 根据Id查询用户
     * @param token
     * @return
     */
    @GetMapping("/findUserInfoById")
    @ApiOperation("根据Id查询用户")
    @ApiResponses({
            @ApiResponse(code = 200,message = "OK",response = UserInfoDto.class)
    })
    public Result findUserInfoById(@RequestHeader String token){
        Integer userId = tokenUtil.getUserId(token);
        UserInfoDto dto=userInfoService.findUserInfoById(userId);
        return Result.success("查询用户成功",dto);
    }
}

